package com.muyu.service.impl;

import com.muyu.entity.UserEntity;
import com.muyu.service.UserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName: UserServiceImpl
 * @Author: Zirun.Xin
 * @Date: 2025/1/9 9:31
 * @Version: 1.0
 * @Description: 用户业务实现层
 */
@Service
public class UserServiceImpl implements UserService {

    /**
     * 获取用户信息
     * @return 查询内容
     */
    @Override
    public List<UserEntity> getUserList() {
        List<UserEntity> userEntityList = new ArrayList<>();
        setSource(userEntityList);

        return userEntityList;
    }

    /**
     * 根据多个id等参数联合查询用户信息
     * @param userIds 多个id集
     * @return 查询内容
     */
    @Override
    public List<UserEntity> getUserByIds(List<Integer> userIds) {
        List<UserEntity> userEntityList = new ArrayList<>();
        setSource(userEntityList);
        return userEntityList.stream()
                .filter(userEntity -> userIds.contains(userEntity.getId()))
                .collect(Collectors.toList());
    }

    /**
     * 根据性别查询用户信息
     * @param sex 性别
     * @return 查询内容
     */
    @Override
    public List<UserEntity> getUserBySex(String sex) {
        List<UserEntity> userEntityList = new ArrayList<>();
        setSource(userEntityList);

        return userEntityList.stream()
                .filter(userEntity -> Objects.equals(sex, userEntity.getSex()))
                .collect(Collectors.toList());
    }

    /**
     * 设置数据源
     * @param list 数据容器
     */
    private void setSource(List<UserEntity> list) {
        list.add(
                new UserEntity(1, "刘亦菲", 18, "女", "上海闵行", "这是db1的用户")
        );
        list.add(
                new UserEntity(2, "杨幂", 20, "女", "北京朝阳", "这是db2的用户")
        );
        list.add(
                new UserEntity(3, "彭于晏", 30, "男", "河北邯郸", "这是db3的用户")
        );
        list.add(
                new UserEntity(4, "胡歌", 22, "男", "江西南昌", "这是db4的用户")
        );
        list.add(
                new UserEntity(5, "刘德华", 30, "男", "河北邯郸", "这是db5的用户")
        );
    }
}
